package leetcode.Hot100;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Cheng Jun
 * Description: 深度优先搜索
 * @version 1.0
 * @date 2021/11/30 22:11
 * 二刷
 */
public class letterCombinations1 {
    static String[] letters = new String[]{"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
    static List<String> resultList = new ArrayList<>();
    static StringBuilder temp = new StringBuilder();
    static String target;

    public static void main(String[] args) {
        System.out.println(letterCombinations("23"));
    }

    public static List<String> letterCombinations(String digits) {
        if (digits == null || digits.length() == 0) return resultList;
        else {
            target = digits;
            backtrack(0);
        }
        return resultList;
    }

    public static void backtrack(int index) {
        if (index == target.length()) {
            resultList.add(temp.toString());
        } else {
            String letter = letters[target.charAt(index) - '0'];
            for (int i = 0; i < letter.length(); i++) {
                temp.append(letter.charAt(i));
                backtrack(index + 1);
                temp.deleteCharAt(temp.length() - 1);
            }
        }
    }
}


